/*   * TODO put header   */package com.byq.mm.smartlib.obd.commands;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/** * TODO put description */
public class ObdMultiCommand implements ObdCommand{
	private List<AbstractObdCommand> commands;

	public void setCommands(List<AbstractObdCommand> commands) {
		this.commands = commands;
	}

	public List<AbstractObdCommand> getCommands() {
		return commands;
	}

	/** * Default ctor. */
	public ObdMultiCommand() {
		this.commands = new ArrayList<AbstractObdCommand>();
	}

	/** * Add ObdCommand to list of ObdCommands. * * @param command */
	public void add(AbstractObdCommand command) {
		this.commands.add(command);
	}

	/** * Removes ObdCommand from the list of ObdCommands. * @param command */
	public void remove(ObdCommand command) {
		this.commands.remove(command);
	}



	/** * * @return */
	public String getFormattedResult() {
		StringBuilder res = new StringBuilder();
		for (ObdCommand command : commands) {
			res.append(command.getFormattedResult()).append(",");
		}
		return res.toString();
	}

	@Override
	public void run(InputStream in, OutputStream out) throws IOException,
			InterruptedException {
		for (ObdCommand command : commands) { /*                            * Send command and read response. */
			command.run(in, out);
		}
		
	}

	@Override
	public String getName() {
		StringBuilder res = new StringBuilder();
		for (ObdCommand command : commands) {
			res.append(command.getName()).append(",");
		}
		return res.toString();
	}

	@Override
	public byte[] getByteResult() {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		for (ObdCommand command : commands) {
			try {
				out.write(command.getByteResult());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return out.toByteArray();
	}

	@Override
	public String getRequestKey() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getResponseKey() {
		// TODO Auto-generated method stub
		return null;
	}
}